﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Money.Interface
{
    public partial class NewDescriptionForm : Form
    {
        private string _description;

        public string Replacement { get; protected set; }
        public string Description 
        {
            get
            {
                return _description;
            }
            set
            {
                _description = _originalDescription.Text = value;
            }
        }
        public bool RegularExpression { get; protected set; }
        public bool Save { get; protected set; }

        public NewDescriptionForm()
        {
            Replacement = "";
            RegularExpression = false;
            Save = false;

            InitializeComponent();
        }

        private void tbNewDescription_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;

            if (string.IsNullOrWhiteSpace(tb.Text))
            {
                _updateButton.Enabled = false;
            }
            else
            {
                _updateButton.Enabled = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save = _save.Checked;
            RegularExpression = _isRegularExpression.Checked;
            Replacement = _newDescription.Text;
            Description = _originalDescription.Text;
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void cbRegEx_CheckedChanged(object sender, EventArgs e)
        {
            if (_isRegularExpression.Checked)
            {
                lbDescription.Text = "Regular Expression";
            }
            else
            {
                lbDescription.Text = "Description";
            }
        }

        private void cbRegEx_CheckedChanged_1(object sender, EventArgs e)
        {
            _save.Enabled = _isRegularExpression.Checked;
            _save.Checked = false;
            lbSave.Enabled = _save.Enabled;
            _originalDescription.ReadOnly = !_isRegularExpression.Checked;
        }

        private void tbBaseDescription_TextChanged(object sender, EventArgs e)
        {
            if (_isRegularExpression.Checked)
            {
                if (!Regex.IsMatch(_description, _originalDescription.Text))
                {
                    _originalDescription.BackColor = Color.Red;
                }
                else
                {
                    _originalDescription.BackColor = Color.White;
                }
            }
        }
    }
}
